<?php


namespace app\mobile\service\base;


use app\admin\model\base\Paper;
use app\admin\model\base\Paper as Question;
use app\admin\model\base\StudentPaper;
use app\BaseService;
use think\response\Json;

class PaperService extends BaseService
{
    /**
     * 列表
     * @param array $params
     * @return Json
     */
    public function index(array $params): Json
    {
        $where = ['is_del'=>0,'is_release'=>1];
        //获取列表
        $list = Paper::where('is_del','=',0)
            ->where('is_release','=',1)
            ->field('student,id')
            ->select()->toArray();
        $arr = [];
        $arr[] = 999999999;
        if(!empty($list)){
            foreach ($list as $value){
                if(is_string($value['student'])){
                    $a = json_decode($value['student']);
                    if(in_array(MOBILE_USER_ID,$a)){
                        $arr[] = $value['id'];
                    }
                }
            }
        }
        $paperList = Paper::where('is_del','=',0)
            ->where('is_release','=',1)
            ->where('id','in',implode(',',$arr))
            ->with(['paper_subjects','special'])
            ->order('id','desc')
            ->paginate(['list_rows' => $params['limit'] ?? 10, 'page' => $params['page'] ?? 1], false)->toArray();
        if(!empty($paperList) && !empty($paperList['data'])){
            foreach ($paperList['data'] as $k=>$datum){
                $subject_num = count($datum['paper_subjects']);
                $subject_score = 0;
                if(!empty($item['paper_subjects'])){
                    foreach ($item['paper_subjects'] as $subject){
                        $subject_score+= $subject['weighting'];
                    }
                }
                $paperList['data'][$k] = [
                    'name'=>$datum['name'],
                    'end_time'=>$datum['end_time'],
                    'subject_num'=>$subject_num,
                    'subject_score'=>$subject_score,
                    'img_url'=>$datum['img_url'],
                    'special_name'=>$datum['special']['name'],
                ];
            }
        }
        return jsonReturn(1, $paperList);
    }

    /**
     * 列表
     * @param array $params
     * @return Json
     */
    public function lists(array $params): Json
    {
        $where = [];
        $where[] = ['student_id','=',MOBILE_USER_ID];
        $list = Paper::where('is_del','=',0)
            ->where('is_release','=',1)
            ->field('student,id')
            ->select()->toArray();
        $arr = [];
        $arr[] = 999999999;
        if(!empty($list)){
            foreach ($list as $value){
                if(is_string($value['student'])){
                    $a = json_decode($value['student']);
                    if(in_array(MOBILE_USER_ID,$a)){
                        $arr[] = $value['id'];
                    }
                }
            }
        }
        //获取练习
        $practiseList = Paper::where('is_del','=',0)
            ->where('is_release','=',1)
            ->where('id','in',implode(',',$arr))
//            ->with(['practise'])
            ->order('id','desc')
            ->paginate(['list_rows' => $params['limit'] ?? 10, 'page' => $params['page'] ?? 1], false)->toArray();
        //查询用户是否参加考试
        $list = StudentPaper::where($where)->select()->toArray();
        $arr = [];
        if(!empty($list)){
            foreach ($list as $item){
                $arr[$item['paper_id']] = $item;
            }
        }
        if(!empty($practiseList['data'])){
            foreach ($practiseList['data'] as $k=>$datum){
                $practiseList['data'][$k]['paper'] = $arr[$datum['id']] ?? [];
            }
        }
        return jsonReturn(1, $practiseList);
    }

    /**
     * 详情
     * @param array $params
     * @return Json
     */
    public function read(array $params): Json
    {
        $where = ['id'=>$params['id']];
        $info = Question::where($where)->with(['paper_subjects','special'])->find();
        if($info){
            $info = $info->toArray();
            $info['count_score'] = 0;
            if(!empty($info['paper_subjects'])){
                foreach ($info['paper_subjects'] as $k=>$item){
                    unset($info['paper_subjects'][$k]['analysis']);
                    unset($info['paper_subjects'][$k]['analysis_desc']);
                    $arr = json_decode($item['content'],true);
                    if($item['type'] == 1 || $item['type'] == 2){
                        if(isset($arr['list']) && !empty($arr['list'])){
                            foreach ($arr['list'] as $kk=>$value){
                                unset($arr['list'][$kk]['is_answer']);
                            }
                        }
                    }
                    $info['count_score']+= $item['weighting'];
                    $info['paper_subjects'][$k]['content'] = $arr;
                }
            }
        }
        return jsonReturn(1, $info);
    }
}